package com.rayboo.example.collections.iterator;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 * ListIterator 示例
 *
 * @author ray
 * @since 2019/07/25
 *
 * <p>ListIterator，它允许我们在两个方向（向前和向后）遍历列表。
 *
 * <p>https://beginnersbook.com/2014/06/listiterator-in-java-with-examples/
 */
public class ListIteratorExample {

    public static void main(String a[]){
        ListIterator<String> listIterator = null;
        List<String> names = new ArrayList<String>();
        names.add("Shyam");
        names.add("Rajat");
        names.add("Paul");
        names.add("Tom");
        names.add("Kate");

        //Obtaining list iterator
        listIterator=names.listIterator();

        System.out.println("Traversing the list in forward direction:");
        while(listIterator.hasNext()){
            System.out.println(listIterator.next());
        }

        System.out.println("\nTraversing the list in backward direction:");
        while(listIterator.hasPrevious()){
            System.out.println(listIterator.previous());
        }
    }
}
